import 'package:flutter/material.dart';
import 'package:hxdj/common/components/underline_tab.dart';
import 'package:hxdj/common/my_header_global.dart';
import 'package:hxdj/home/order/order_item_page.dart';
import 'package:hxdj/home/order/order_manager.dart';

class OrderPage extends StatefulWidget {
  static const String routeName = '/orderPage';

  const OrderPage({super.key});

  @override
  State<OrderPage> createState() => _OrderPageState();
}

class _OrderPageState extends State<OrderPage> with TickerProviderStateMixin {
  List<String> tabs = ['全部', '待消费', '已完成', '已取消'];
  List<String> orderStatus = [
    OrderManager.orderAllList,
    OrderManager.orderConsumeList,
    OrderManager.orderFinishList,
    OrderManager.orderCancelList,
  ];

  // 每个标签页的键，确保每个页面的状态被独立保存
  final List<Key> _tabKeys = <Key>[
    UniqueKey(),
    UniqueKey(),
    UniqueKey(),
    UniqueKey(),
    // 添加更多的键以支持更多的标签页
  ];


  late TabController? _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: MyColours.materialBg, appBar: const MyAppBar(title: "全部订单"), body: _buildListTab());
  }

  List<Widget> _buildTabWidget() {
    List<Widget> tabsWidget = [];

    tabsWidget = tabs
        .map((e) => CATab(
              text: e
            ))
        .toList();
    return tabsWidget;
  }

  Widget _buildListTab() {
    return Column(
      children: [
        Container(
          color: Colors.white,
          child: TabBar(
            controller: _tabController,
            dividerColor: Colors.transparent,
            labelColor: Colors.black,
            unselectedLabelColor: Colors.black,
            labelStyle: const TextStyle(fontSize: 14, fontWeight: FontWeight.w600),
            unselectedLabelStyle: const TextStyle(fontSize: 14, fontWeight: FontWeight.normal),
            indicator: const CAUnderlineTabIndicator(
                indicatorBottom: 4,
                indicatorWidth: 40,
                isRound: true,
                borderSide: BorderSide(
                  width: 3,
                  color: MyColours.appMain,
                )),
            padding: const EdgeInsets.only(bottom: 2),
            indicatorPadding: EdgeInsets.zero,
            tabs: _buildTabWidget(),
          ),
        ),
        Expanded(
            child: TabBarView(
                key: GlobalKey(),
                controller: _tabController,
                children: orderStatus
                    .map((e) => OrderItemPage(
                          orderStatus: e,
                          key: _tabKeys[orderStatus.indexOf(e)],
                        ))
                    .toList())),
      ],
    );
  }
}
